package exabase.data;

import static org.junit.Assert.assertEquals;

import java.util.Iterator;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DataStoreTest {

	private Hash hash1 = new Hash("hash1");
	private Hash hash2 = new Hash("hash2");
	private String testFile1Name = "testFiles/testFile1.txt";
	private Hash testFile1Hash = new Hash("6438f31616e9aba886a4c4820a4aaba4");
	private String fileName1 = "fileName1";
	private String fileName2 = "fileName2";

	@Before
	public void setUp() throws Exception {
		DataStore.reset();
	}

	@After
	public void tearDowm() throws Exception {
		DataStore.reset();
	}

	@Test
	public void testAddData() {
		DataStore.getInstance().addData("fileURL", hash1);
		assertEquals("there should be one data", 1, DataStore.getInstance()
				.getDataSet().size());
		assertEquals("the fileURL should be correct", "fileURL", DataStore
				.getInstance().getDataSet().iterator().next().getFileURL());
		assertEquals("the hash should be correct", hash1, DataStore
				.getInstance().getDataSet().iterator().next().getHash());
	}

	@Test
	public void testAddData2() {
		DataStore.getInstance().addData(fileName1, hash1);
		DataStore.getInstance().addData(fileName2, hash2);
		assertEquals("there should be two data", 2, DataStore.getInstance()
				.getDataSet().size());

		Iterator<Data> it = DataStore.getInstance().getDataSet().iterator();

		Data data1 = it.next();
		Data data2 = it.next();
		;
		if (data1.getFileURL().equals(fileName2)) {
			Data tmp = data1;
			data1 = data2;
			data2 = tmp;
		}

		assertEquals("the fileURL should be correct", fileName1, data1
				.getFileURL());
		assertEquals("the hash should be correct", hash1, data1.getHash());

		assertEquals("the fileURL should be correct", fileName2, data2
				.getFileURL());
		assertEquals("the hash should be correct", hash2, data2.getHash());
	}

	@Test
	public void testAddFile() {
		DataStore.getInstance().addFile(testFile1Name);
		assertEquals("there should be two data", 1, DataStore.getInstance()
				.getDataSet().size());
		assertEquals("the fileURL should be correct",
				"testFiles/testFile1.txt", DataStore.getInstance().getDataSet()
						.iterator().next().getFileURL());
		assertEquals("the hash should be correct", testFile1Hash, DataStore
				.getInstance().getDataSet().iterator().next().getHash());
	}
}
